package com.ky.pro.java.rpc.hd.common.service.impl;

import com.ky.pro.java.rpc.hd.common.entity.Account;
import com.ky.pro.java.rpc.hd.common.entity.FrozenAssets;
import com.ky.pro.java.rpc.hd.common.repository.AccountRepository;
import com.ky.pro.java.rpc.hd.common.repository.FrozenAssetsRepository;
import com.ky.pro.java.rpc.hd.common.web.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @ClassName DollarServiceImpl
 * @Description TODO
 * @Date 2022/1/2 21:21
 * @Author kevin
 **/
@Service
public class ExchangeBaseServiceImpl {

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    FrozenAssetsRepository frozenAssetsRepository;

    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> freezeAssets(long accountId, int currency, long amount) {
        // 从账户中扣减金额并添加到冻结表
        boolean retStatus = accountRepository.updateBalanceByAccountId(accountId, currency, -amount);

        // 同一账户、同一币种只有一条冻结记录
        FrozenAssets frozenAssets = frozenAssetsRepository.findFrozenAssetsByAccountIdAndCurrency(accountId, currency);
        if (frozenAssets == null) {
            retStatus = retStatus & frozenAssetsRepository.save(FrozenAssets.builder()
                    .accountId(accountId)
                    .amount(amount)
                    .currency(currency)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .version(0)
                    .delStatus(0)
                    .build());
        } else {
            retStatus = retStatus & frozenAssetsRepository.updateFrozenAssetsByAccountId(accountId, currency, amount);
        }
        return ResultBean.genResult(retStatus);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> exchange(long accountId, long amount, int fromCurrency, int toCurrency, double ratio) {
        // 从冻结表减少冻结金额
        boolean retStatus = frozenAssetsRepository.updateFrozenAssetsByAccountId(accountId, fromCurrency, -amount);
        // 在RMB账户中添加金额
        Account rmbAccount = accountRepository.findAccountRecordsByAccountIdAndCurrency(accountId, toCurrency);

        long newBalance = (long) Math.ceil(amount * ratio);

        if (rmbAccount == null) {

            Account dollarAccount = accountRepository.findAccountRecordsByAccountIdAndCurrency(accountId, fromCurrency);
            if (dollarAccount == null) {
                throw new RuntimeException("没有找到任何账号信息，请先创建账号！");
            }

            retStatus = retStatus & accountRepository.save(Account.builder()
                    .accountId(accountId)
                    .accountPwd(dollarAccount.getAccountPwd())
                    .userName(dollarAccount.getUserName())
                    .salt(dollarAccount.getSalt())
                    .balance(newBalance)
                    .currency(toCurrency)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .version(0)
                    .delStatus(0)
                    .build());
        } else {
            retStatus = retStatus & accountRepository.updateBalanceByAccountId(accountId, toCurrency, newBalance);
        }
        return ResultBean.genResult(retStatus);
    }
}
